package com.spider.runnable;

import com.spider.entites.Task;
import org.apache.commons.dbutils.QueryRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.Pipeline;
import us.codecraft.webmagic.processor.PageProcessor;

import java.sql.SQLException;

/**
 * @author panglin
 * @date 2020/12/1 19:33
 */
@Component
@Scope("prototype")
public class ListPageRunnable implements Runnable {

    private static final Logger LOGGER = LoggerFactory.getLogger(ListPageRunnable.class);

    private QueryRunner queryRunner;

    private PageProcessor pageProcessor;

    private Pipeline pipeline;

    private Task task;

    private Integer threadNum;

    public Runnable initTask(QueryRunner queryRunner,PageProcessor processor,Pipeline pipeline,Task task,Integer num){
        this.queryRunner = queryRunner;
        this.pageProcessor = processor;
        this.pipeline = pipeline;
        this.task = task;
        this.threadNum = num;
        return this;
    }

    @Override
    public void run() {
        this.listStarter(this.pageProcessor, this.pipeline, this.task.getIndexPage(), this.threadNum);
    }

    /**
     * 同步启动
     * @param processor
     * @param pipeline
     * @param url
     * @param threadNum
     */
    private void listStarter(PageProcessor processor, Pipeline pipeline, String url, Integer threadNum) {
        LOGGER.info("列表页任务已开始,地址:{}", url);
        Spider.create(processor)
                .addUrl(url)
                .addPipeline(pipeline)
                .thread(threadNum)
                .run();
        this.finish();
    }

    /**
     * 列表爬取完毕，更新爬取数量（总爬取数以列表页为准），更新详情页执行状态（开始）
     */
    private void finish() {
        Integer total = null;
        try {
            total = this.queryRunner.query("select count(id) from " + this.task.getTbName(), rs -> {
                int t = 0;
                while (rs.next()) {
                    t = rs.getInt(1);
                }
                return t;
            });
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            this.queryRunner.update("update tb_tasks set salveStatus = 0,spiderNum = ? where id=?", total, this.task.getId());
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        LOGGER.info("列表页执行完毕,爬取总数为{},详情页开始", total);
    }
}
